<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="链接" prop="href">
        <el-input
          v-model="queryParams.href"
          placeholder="请输入链接"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="关联文章id" prop="aId">
        <el-input
          v-model="queryParams.aId"
          placeholder="请输入关联文章id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="时间" prop="time">
        <el-input
          v-model="queryParams.time"
          placeholder="请输入时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="是否删除1删除" prop="isDelete">
        <el-input
          v-model="queryParams.isDelete"
          placeholder="请输入是否删除1删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="排序" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item
        label="是否显示在首页"
        prop="isHome"
        class="custom_is_home_box"
      >
        <!-- <el-input
          v-model="queryParams.isHome"
          placeholder="请输入是否首页，默认0,1首页"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.isHome"
          placeholder="请选择"
          class="custom_is_home"
        >
          <el-option
            v-for="item in homeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:multimedia:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:multimedia:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:multimedia:remove']"
          >删除</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="multimediaList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" align="center" width="70">
        <template slot-scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1">视频</span>
          <span v-if="scope.row.type == 2">图片</span>
        </template>
      </el-table-column>
      <el-table-column label="链接" align="center" prop="href">
        <template slot-scope="scope">
          <a style="color: #1890ff" :href="scope.row.href" target="_blank">{{
            scope.row.href
          }}</a>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title">
        <template slot-scope="scope">
          <el-popover placement="top-start" width="400" trigger="hover">
            <span>{{ scope.row.title }}</span>
            <span
              slot="reference"
              style="
                display: inline-block;
                width: 100px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
                color: #1890ff;
              "
            >
              {{ scope.row.title }}
            </span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="source">
        <template slot-scope="scope">
          <el-popover placement="top-start" width="400" trigger="hover">
            <span>{{ scope.row.source }}</span>
            <span
              slot="reference"
              style="
                display: inline-block;
                width: 100px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
                color: #1890ff;
              "
            >
              {{ scope.row.source }}
            </span>
          </el-popover>
        </template>
      </el-table-column>
      <!-- <el-table-column label="关联文章id" align="center" prop="aId" /> -->
      <el-table-column label="时间" align="center" prop="time" />
      <!-- <el-table-column label="是否删除1删除" align="center" prop="isDelete" /> -->
      <el-table-column label="排序" align="center" prop="num" />
      <el-table-column label="是否首页" align="center" prop="isHome">
        <template slot-scope="scope">
          <span v-if="scope.row.isHome == 0">否</span>
          <span v-if="scope.row.isHome == 1">是</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:multimedia:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:multimedia:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改多媒体库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="语言" prop="language">
          <el-select v-model="form.language" placeholder="请选择语种">
            <el-option label="英语" value="EN"></el-option>
            <el-option label="法语" value="FR"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入来源" />
        </el-form-item>
        <!-- <el-form-item label="关联文章id" prop="aId">
          <el-input v-model="form.aId" placeholder="请输入关联文章id" />
        </el-form-item> -->
        <el-form-item label="时间" prop="time">
          <el-input v-model="form.time" placeholder="请输入时间" />
        </el-form-item>
        <!-- <el-form-item label="是否删除1删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入是否删除1删除" />
        </el-form-item> -->
        <el-form-item label="排序" prop="num">
          <el-input v-model="form.num" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="是否首页" prop="isHome">
          <el-radio-group v-model="form.isHome">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="多媒体类型" prop="type">
          <el-radio-group v-model="form.type" @change="changeRadioIsBan">
            <el-radio :label="1">视频</el-radio>
            <el-radio :label="2">图片</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上传视频" prop="href" v-if="flagBigImg == 1">
          <!-- <input type="file" @change="selectVideo" />
          <button @click="uploadVideo">上传视频</button> -->
          <file-upload v-model="form.href" />
        </el-form-item>
        <el-form-item label="上传图片" prop="href" v-if="flagBigImg == 2">
          <image-upload v-model="form.href" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMultimedia,
  getMultimedia,
  delMultimedia,
  addMultimedia,
  updateMultimedia,
} from "@/api/system/multimedia";

export default {
  name: "Multimedia",
  data() {
    return {
      homeOptions: [
        {
          value: 0,
          label: "否",
        },
        {
          value: 1,
          label: "是",
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 多媒体库表格数据
      multimediaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        href: null,
        title: null,
        source: null,
        aId: null,
        time: null,
        isDelete: null,
        num: null,
        isHome: null,
        language: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      // rules: {},
      flagBigImg: 0,
      selectedVideo: null,
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        language: [
          { required: true, message: "请选择语种", trigger: "change" },
        ],
        source: [{ required: true, message: "请输入来源", trigger: "blur" }],
        time: [{ required: true, message: "请输入时间", trigger: "blur" }],
        // num: [{ required: true, message: "请输入排序", trigger: "blur" }],
        coverImage: [
          {
            required: true,
            message: "请上传图片",
            trigger: "blur",
          },
        ],
        bigImage: [
          {
            required: true,
            message: "请上传大图",
            trigger: "blur",
          },
        ],
        isHome: [
          { required: true, message: "请选择是否首页", trigger: "change" },
        ],
        type: [
          { required: true, message: "请选择多媒体类型", trigger: "change" },
        ],
        // href: [{ required: true, message: "请选择是否置顶", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeRadioIsBan(val) {
      if (val == 1) {
        this.flagBigImg = 1;
      } else {
        this.flagBigImg = 2;
      }
      this.form.href = "";
    },
    /** 查询多媒体库列表 */
    getList() {
      this.loading = true;
      listMultimedia(this.queryParams).then((response) => {
        this.multimediaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        // id: null,
        type: null,
        href: null,
        title: null,
        source: null,
        aId: null,
        time: null,
        // createTime: null,
        // isDelete: null,
        num: null,
        isHome: null,
        language: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加多媒体库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMultimedia(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改多媒体库";
        this.flagBigImg = this.form.type;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMultimedia(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMultimedia(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除多媒体库编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMultimedia(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/multimedia/export",
        {
          ...this.queryParams,
        },
        `multimedia_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.custom_is_home {
  width: 90px;
}
.custom_is_home_box .el-form-item__label {
  width: 111px !important;
}
</style>
